<template>
  <div>
    <span style="color: red">Count数值为：{{num}}</span><br>
    <span>getters获取第一个人：{{getFirstName}}</span><br>
    <input type="text" v-model="name">
    <button @click="handleAdd">添加</button>
    <button @click="handleAddWang">添加一个姓王的人</button>
    <button @click="handleAddService">添加一个随即名字的人</button>
    <ul>
      <li v-for="user in userList" :key="user.id">{{user.name}}</li>
    </ul>
  </div>
</template>

<script>
import { nanoid } from 'nanoid';

export default {
  name: 'PersonView',
  data() {
    return {
      name: ''
    }
  },
  methods: {
    handleAdd() {
      const obj = {
        id: nanoid(),
        name: this.name,
      }
      this.$store.commit('person/saveUser', obj)
      this.name = ''
    },
    handleAddWang() {
      this.$store.dispatch('person/addWang', this.name)
      this.name = '';
    },
    handleAddService() {
      this.$store.dispatch('person/addService', this.name)
    }
  },
  computed: {
    userList() {
      return this.$store.state.person.userList
    },
    num() {
      return this.$store.state.count.num
    },
    getFirstName() {
      return this.$store.getters['person/getFirstName']
    }
  }
}
</script>

<style>

</style>